package com.yinhai.message.core.dao;

import com.yinhai.core.service.ta3.domain.dao.HibernateDAO;
import com.yinhai.message.core.po.MessageFilePo;

import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @author aolei 2017年3月31日11:19:52
 *         <p>
 *         IMessageFileDao 实现类
 */
@Repository("messageFileDao")
public class MessageFileDaoImpl extends HibernateDAO<MessageFilePo> implements IMessageFileDao {

    @Override
    public MessageFilePo save(MessageFilePo po) {
        super.save(po);
        return po;
    }

    @Override
    public void update(MessageFilePo po) {
        super.update(po);
    }

	@Override
	public List<MessageFilePo> queryByMsgid(String msgid) {
		String hql =" from MessageFilePo p where p.msgid = ?";
		return find(hql, msgid);
	}

	@Override
	public MessageFilePo queryByFileid(String fileid) {
		String hql =" from MessageFilePo p where p.id = ?";
		return super.get(hql.toString(),fileid);
	}

	
    @Override
    public void bathchSave(final List<MessageFilePo> filePoList) {
        super.jdbcTemplate.batchUpdate("INSERT INTO TAMESSAGEFILE(id,msgid,filename,filepath,uploadsuccess,filestoremode) VALUES (?,?,?,?,?,?)", new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                MessageFilePo filePo = filePoList.get(i);
                ps.setString(1,filePo.getId());
                ps.setString(2,filePo.getMsgid());
                ps.setString(3,filePo.getFilename());
                ps.setString(4,filePo.getFilepath());
                ps.setString(5,filePo.getUploadsuccess());
                ps.setString(6,filePo.getFilestoremode());
            }

            @Override
            public int getBatchSize() {
                return filePoList.size();
            }
        });
    }

}
